package com.jzl.swing.motherfucker;

import java.io.File;

import com.jzl.swing.dao.SupplierDao;
import com.jzl.swing.dao.impl.SupplierDaoImpl;
import com.jzl.swing.entity.Supplier;

import jxl.*;

public class ReadExcel {
	public static void main(String[] args) {
		int i;
		Sheet sheet;
		Workbook book;
		Cell cell1, cell2, cell3, cell4, cell5, cell6;
		try {
			// hello.xls为要读取的excel文件名
			book = Workbook.getWorkbook(new File("excel_table/2.xls"));

			// 获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
			sheet = book.getSheet(0);
			// 获取左上角的单元格
			cell1 = sheet.getCell(0, 0);
			System.out.println("标题：" + cell1.getContents());

			i = 1;
			while (true) {
				if (i == 1) {
					i++;
					continue;
				}
				// 获取每一行的单元格
				cell1 = sheet.getCell(0, i);// （列，行）
				cell2 = sheet.getCell(1, i);
				cell3 = sheet.getCell(2, i);
				cell4 = sheet.getCell(3, i);
				cell5 = sheet.getCell(4, i);
				cell6 = sheet.getCell(5, i);
				if ("".equals(cell1.getContents()) == true) // 如果读取的数据为空
					break;

				System.out.println(cell1.getContents() + "\t" + cell2.getContents() + "\t" + cell3.getContents() + "\t"
						+ cell4.getContents() + "\t" + cell5.getContents() + "\t" + cell6.getContents());
				
				String supplier_id = cell1.getContents();	// 供应商编号
				String supplier_name = cell2.getContents();	// 供应商名称
				String supplier_disc = cell3.getContents();
				String supplier_contacter = cell4.getContents();
				String supplier_phone = cell5.getContents();
				String supplier_address = cell6.getContents();
				
				Supplier supplier = new Supplier();
				supplier.setSupplier_id(supplier_id);
				supplier.setSupplier_name(supplier_name);
				supplier.setsupplier_describ(supplier_disc);
				supplier.setsupplier_contacter(supplier_contacter);
				supplier.setsupplier_phone(supplier_phone);
				supplier.setsupplier_address(supplier_address);
				SupplierDao supplierDao = new SupplierDaoImpl();
				if (null==supplierDao.queryById(supplier_id)) {
					new SupplierDaoImpl().add(supplier);
				}
				i++;
			}
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}